package Actions;

import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Scrollbar;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class OptionChooser {
	
	public static final int RESULT_OK = 0;
	public static final int RESULT_ERROR = -1;
	
	private static Dialog d;
	
	private TypeWriter.TypeWriter parent;
	
	private Checkbox typewriterScrollOn;
	private Checkbox soundOn;
	private Scrollbar soundVolume;
	private int volume;
	
	public OptionChooser(TypeWriter.TypeWriter parent)
	{
		this.parent = parent;
		
		// Create a modal dialog
		d = new Dialog(this.parent, "Choose Options", true);

		// Use a flow layout
		d.setLayout( new GridLayout(7,1) );
		

		// Create an OK button
		Button ok = new Button ("OK");
		ok.addActionListener ( new ActionListener()
		{
			public void actionPerformed( ActionEvent e )
			{
				// Hide dialog
				OptionChooser.d.setVisible(false);
			}
		});

		soundOn = new Checkbox("Sounds on?");
		typewriterScrollOn = new Checkbox("Typewriter scrolling on?");
		soundVolume = new Scrollbar(Scrollbar.HORIZONTAL);

		d.add( typewriterScrollOn );
		d.add( soundOn );
		d.add( new Label("") );
		d.add( new Label("Sound volume"));
		d.add(soundVolume);
		d.add( new Label("") );
		
		d.add( ok );

		// Show dialog
		d.pack();
		
		// size position
		Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
		d.setLocation(size.width/2 - d.getWidth(), size.height/2 - d.getHeight());
	}
	
	public int show()
	{
		soundVolume.setValue(volume);
		d.setVisible(true);
		volume = soundVolume.getValue();
		return RESULT_OK;
	}
	
	public void setSoundState(boolean set)
	{
		soundOn.setState(set);
	}
	
	public void setScrollState(boolean set)
	{
		typewriterScrollOn.setState(set);
	}
	
	public boolean getSoundState()
	{
		return soundOn.getState();
	}
	
	public boolean getScrollState()
	{
		return typewriterScrollOn.getState();
	}
	
	public int getVolume()
	{
		return volume;
	}
	
	public void disposeDialog()
	{
		d.dispose();
	}
	
}